function[data] = read_iaga2002_1_sec(fname)
% Reads 1 second magnetic data (XYZ) in
% IAGA20002 format

fid = fopen(fname,'r');

if fid == -1,
    sprintf('Unable to open <%s>',fname);
end

[S] = fgetl(fid);%  Format                 IAGA-2002
[S] = fgetl(fid);%  Source of Data         IPGP                                         |
name = fgetl(fid);%  Station Name           ADDIS ABABA                                  |
[S] = fgetl(fid);%  IAGA CODE              AAE                                          |
ID = sscanf(S,'%*s%*s%s');
[S] = fgetl(fid);%  Geodetic Latitude      09.033                                       |
LAT = sscanf(S,'%*s%*s%f');
[S] = fgetl(fid);%  Geodetic Longitude     038.767                                      |
LON = sscanf(S,'%*s%*s%f');
[S] = fgetl(fid);%  Elevation              2442                                         |
[S] = fgetl(fid);%  Reported               XYZF                                         |
coords0 = sscanf(S,'%*s%s');
[S] = fgetl(fid);%  Sensor Orientation     HDZF                                         |
[S] = fgetl(fid);%  Digital Sampling       1 minute
sampling = sscanf(S,'%*s%*s%s');
[S] = fgetl(fid);%  Data Interval Type     Filtered 1-minute (00:30 - 01:29)            |
[S] = fgetl(fid);%  Data Type              definitive                                   |
[S] = fgetl(fid);%  This file was converted from the INTERMAGNET CD-ROM binary data   |
fclose(fid);


[date,hms,~,X,Y,Z, F] = textscan(fname,'%s %s %f %f %f %f %f','headerlines',21,'delimiter',' ');
%deal with the missing data
X(X==99999) = NaN;
Y(Y==99999) = NaN;
Z(Z==99999) = NaN;
F(F==99999 | F==88888) = NaN;

startfday =datenum(date(1)) + datenum(hms(1));
MINUTELY(:,1) = startfday:(1/(60*1440)):(startfday + (length(X) - 1) /(60*1440));
%
MINUTELY(:,[2 3 4 5]) = [X Y Z F];
data = struct('MINUTELY',MINUTELY);
data.LAT = LAT;
data.LON = LON;
data.CODE = ID;
data.name = name;




%------------------------------------

